package com.zd.kids.ui.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;

import com.blankj.utilcode.constant.PermissionConstants;
import com.blankj.utilcode.util.PermissionUtils;
import com.blankj.utilcode.util.SPUtils;
import com.gyf.immersionbar.BarHide;
import com.gyf.immersionbar.ImmersionBar;
import com.hyy.language.CommonUtils;
import com.hyy.language.LanguageDataBean;
import com.hyy.language.SelectLanguageDialog;
import com.hyy.libray.common.Constant;
import com.hyy.libray.presenter.IBasePresenter;
import com.hyy.libray.ui.activity.BaseActivity;
import com.zd.kids.R;
import com.zd.kids.common.MyApplication;
import com.zd.kids.ui.view.QuquesPermissionDialog;

import java.util.List;
import java.util.concurrent.TimeUnit;

import androidx.annotation.NonNull;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class SplashActivity extends BaseActivity {

    QuquesPermissionDialog basePopupView;

    /**
     * 获取布局文件地址
     */
    @Override
    public int getLayoutResId() {
        return R.layout.activity_splash;
    }

    /**
     * 初始化布局
     */
    @Override
    public void initView() {
        ImmersionBar.with(this)
                .fullScreen(true)
                .hideBar(BarHide.FLAG_HIDE_BAR)
                .init();
        if (PermissionUtils.isGranted(PermissionConstants.CAMERA, PermissionConstants.MICROPHONE, PermissionConstants.STORAGE)) {
            if (!TextUtils.isEmpty(CommonUtils.getCurReleaseLanguage())) {
                Observable.timer(1500, TimeUnit.MICROSECONDS)
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribeOn(Schedulers.io())
                        .subscribe(aLong -> {
                            goToHome();
                        });
            } else {
                showSelectLanguage();
            }
        } else {
            goPermission(PermissionConstants.CAMERA, PermissionConstants.MICROPHONE, PermissionConstants.STORAGE);
        }

    }

    private boolean isFirstShow = true;

    @Override
    protected void onResume() {
        super.onResume();

        if (isFirstShow) {
            isFirstShow = false;
        } else {
            if (PermissionUtils.isGranted("android.permission.CAMERA", "android.permission.READ_EXTERNAL_STORAGE", "android.permission.RECORD_AUDIO", "android.permission.WRITE_EXTERNAL_STORAGE")) {
                goToHome();
            }
        }

    }

    /**
     * 获取Presenter，不用MVP，可以为空
     *
     * @return
     */
    @Override
    protected IBasePresenter onBasePresenter() {
        return null;
    }


    private void goPermission(@PermissionConstants.PermissionGroup String... permissions) {
        PermissionUtils.permission(permissions)
                .callback(new PermissionUtils.SingleCallback() {
                    @Override
                    public void callback(boolean isAllGranted, @NonNull List<String> granted, @NonNull List<String> deniedForever, @NonNull List<String> denied) {
                        if (isAllGranted) {
                            goToHome();
                        } else {
                            if (deniedForever.size() > 0) {
                                showPermissionTips();
                            } else if (denied.size() > 0) {
                                goPermission(denied.toArray(new String[denied.size()]));
                            }
                        }
                    }
                }).request();
    }


    private void showPermissionTips() {
        if (basePopupView == null) {
            basePopupView = new QuquesPermissionDialog(this);
        }
        if (basePopupView != null && !basePopupView.isShow()) {
            basePopupView.showView();
        }

    }

    private SelectLanguageDialog selectLanguageDialog;

    private void showSelectLanguage() {
        if (selectLanguageDialog == null) {
            selectLanguageDialog = new SelectLanguageDialog(this);
            selectLanguageDialog.setOnclinckCallBack(new SelectLanguageDialog.OnclinckCallBack() {
                @Override
                public void onclinckCallBack(LanguageDataBean selectLanguage) {
                    goToHome();
                }
            });
            selectLanguageDialog.setData(getString(R.string.select_language), getString(R.string.queren));
        }
        selectLanguageDialog.show();
    }

    private void goToHome() {
        if (TextUtils.isEmpty(CommonUtils.getCurReleaseLanguage())) {
            showSelectLanguage();
            return;
        }
        String jwt = SPUtils.getInstance(Constant.GAPSK_AI_QM).getString("jwt");
        Intent intent;
        if (TextUtils.isEmpty(jwt)) {
            intent = new Intent(SplashActivity.this, WelcomeActivity.class);
        } else {
            intent = new Intent(SplashActivity.this, MainTypeActivity.class);
        }
        if (MyApplication.isHasFlutter) {
//            intent = FlutterUtils.getFultterIntent(this);
        }
        startActivity(intent);
        finish();
    }


//    public void getLanguageTypes() {
//        DataManager.service.getLanguageGroup()
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Observer<HttpResponse<List<LanguageDataBean>>>() {
//                    @Override
//                    public void onSubscribe(@io.reactivex.annotations.NonNull Disposable d) {
//                        Log.i("getLanguageTypes", "onSubscribe: ====>");
//                    }
//
//                    @Override
//                    public void onNext(@io.reactivex.annotations.NonNull HttpResponse<List<LanguageDataBean>> listHttpResponse) {
//                        if (listHttpResponse.getData() != null) {
//                            setLanguageDatas(listHttpResponse.getData());
//                        }
//                    }
//
//                    @Override
//                    public void onError(@io.reactivex.annotations.NonNull Throwable e) {
//                        Log.i("getLanguageTypes", "onNext: ====>");
//                    }
//
//                    @Override
//                    public void onComplete() {
//                        Log.i("getLanguageTypes", "onComplete: ====>");
//                    }
//                });
//    }
}
